package com.hu.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.auth.common.exception.CustomException;
import com.hu.auth.common.result.RCodeEnum;
import com.hu.auth.common.utils.MenuUtil;
import com.hu.auth.mapper.MenuMapper;
import com.hu.auth.mapper.RoleMenuMapper;
import com.hu.auth.model.Menu;
import com.hu.auth.model.RoleMenu;
import com.hu.auth.model.po.AssignMenuPo;
import com.hu.auth.service.MenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Resource
    MenuMapper menuMapper;
    @Resource
    RoleMenuMapper roleMenuMapper;

    // 查询全部做分拣
    @Override
    public List<Menu> tree() {
        return MenuUtil.sort(menuMapper.selectList(null), 0L);
    }

    @Transactional
    @Override
    public void removeById(Long id) {
        // 查看是否还有子菜单
        Integer count = menuMapper.selectCount(new LambdaQueryWrapper<Menu>().eq(Menu::getParentId, id));
        if (count > 0) {
            throw new CustomException(RCodeEnum.NODE_ERROR.getCode(), RCodeEnum.NODE_ERROR.getMessage());
        }
        // 删除菜单
        menuMapper.deleteById(id);
    }

    // 查询角色的菜单
    @Override
    public List<Menu> getByRoleId(Long roleId) {
        // 查看所有的菜单
        List<Menu> allMenuList = menuMapper.selectList(null);
        // 查看角色已经选择的菜单
        List<Long> checkMenuIdList = roleMenuMapper.selectList(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, roleId))
                .stream()
                .map(RoleMenu::getMenuId)
                .collect(Collectors.toList());

        // 对已经选择的菜单进行标记
        allMenuList.forEach(menu -> {
            if (checkMenuIdList.contains(menu.getId())) {
                menu.setIsSelect(true);
            }
        });
        return MenuUtil.sort(allMenuList, 0L);
    }

    // 为角色分配菜单
    @Transactional
    @Override
    public void assignMenu(AssignMenuPo po) {
        // 删除原来的
        roleMenuMapper.delete(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, po.getRoleId()));
        // 添加
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(po.getRoleId());
        po.getMenuIdList().forEach(menuId -> {
            roleMenu.setMenuId(menuId);
            roleMenuMapper.insert(roleMenu);
        });
    }

    // 逐次查询
    // @Override
    // public List<Menu> tree() {
    //     List<Menu> oneLevelMenu = menuMapper.selectList(new LambdaQueryWrapper<Menu>().eq(Menu::getParentId, 0));
    //     oneLevelMenu.forEach(menu -> menu.setMenuList(getNextLevelTree(menu.getId())));
    //     return oneLevelMenu;
    // }
    // private List<Menu> getNextLevelTree(Long menuId) {
    //     List<Menu> menuList = menuMapper.selectList(new LambdaQueryWrapper<Menu>().eq(Menu::getParentId, menuId));
    //     if (menuList.size() == 0) {
    //         return null;
    //     }
    //     menuList.forEach(menu -> menu.setMenuList(getNextLevelTree(menu.getId())));
    //     return menuList;
    // }

}
